﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace TDLib.DB
{
    public class AccessDBHelper
    {
        private string m_dbconnstr;

        public AccessDBHelper(string dbfilepath)
        {
            m_dbconnstr = "provider=microsoft.jet.oledb.4.0;data source=" + dbfilepath;
        }

        /// <summary>
        /// 从数据库中执行某一SQL命令，不返回数据集合(用指定的连接名)
        /// </summary>
        public int RunSQL(string strSQL)
        {
            int intResult = 0;

            OleDbConnection conn = new OleDbConnection(m_dbconnstr);
            conn.Open();

            OleDbCommand cmd = new OleDbCommand(strSQL, conn);

            try
            {
                intResult = cmd.ExecuteNonQuery();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }            

            return intResult;
        }

        /// <summary>
        /// 从数据库中执行某一SQL命令，不返回数据集合(用指定的连接名)
        /// </summary>
        public int RunSQL(OleDbCommand cmd)
        {
            int intResult = 0;

            OleDbConnection conn = new OleDbConnection(m_dbconnstr);
            conn.Open();

            cmd.Connection = conn;

            try
            {
                intResult = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }       

            conn.Close();

            return intResult;
        }

        /// <summary>
        /// 执行sql语句，并返回dataset
        /// </summary>
        /// <param name="strSQL"></param>
        /// <returns></returns>
        public DataSet RunSQLDS(string strSQL)
        {
            OleDbConnection conn = new OleDbConnection(m_dbconnstr);

            OleDbCommand cmd = new OleDbCommand(strSQL, conn);
            OleDbDataAdapter dad = new OleDbDataAdapter(cmd);

            DataSet ds = new DataSet();

            dad.Fill(ds);

            return ds;

        }

        /// <summary>
        /// 执行sql语句，并返回dataset
        /// </summary>
        public DataSet RunSQLDS(OleDbCommand cmd)
        {
            OleDbConnection conn = new OleDbConnection(m_dbconnstr);

            cmd.Connection = conn;
            OleDbDataAdapter dad = new OleDbDataAdapter(cmd);

            DataSet ds = new DataSet();

            dad.Fill(ds);

            return ds;

        }

    }
}
